﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using rock.UI.common;

namespace rock.DAL
{
	/// <summary>
	/// 数据访问类:DalFEE_SHAOYAJING
	/// </summary>
	public partial class DalFEE_SHAOYAJING
	{
		public DalFEE_SHAOYAJING()
		{}
		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
        public bool Exists(int SEQID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from FEE_SHAOYAJING");
			strSql.Append(" where SEQID=@SEQID ");
			SqlParameter[] parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4)			};
			parameters[0].Value = SEQID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(rock.Model.ModelFEE_SHAOYAJING model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into FEE_SHAOYAJING(");
			strSql.Append("RENT_CONTRACT_SEQID,MONEY,RENT_CUSTOM_SEQID,TPYE,OPERA_MAN,OPERA_TIME,RECORD_TIME,STATUS,NOTE)");
			strSql.Append(" values (");
			strSql.Append("@RENT_CONTRACT_SEQID,@MONEY,@RENT_CUSTOM_SEQID,@TPYE,@OPERA_MAN,@OPERA_TIME,@RECORD_TIME,@STATUS,@NOTE)");
			SqlParameter[] parameters = {
				
					new SqlParameter("@RENT_CONTRACT_SEQID", SqlDbType.Int,4),
					new SqlParameter("@MONEY", SqlDbType.Decimal,9),
					new SqlParameter("@RENT_CUSTOM_SEQID", SqlDbType.Int,4),
					new SqlParameter("@TPYE", SqlDbType.VarChar,30),
					new SqlParameter("@OPERA_MAN", SqlDbType.VarChar,30),
					new SqlParameter("@OPERA_TIME", SqlDbType.DateTime),
					new SqlParameter("@RECORD_TIME", SqlDbType.DateTime),
					new SqlParameter("@STATUS", SqlDbType.Int,4),
					new SqlParameter("@NOTE", SqlDbType.VarChar,300)};
			
			parameters[0].Value = model.RENT_CONTRACT_SEQID;
			parameters[1].Value = model.MONEY;
			parameters[2].Value = model.RENT_CUSTOM_SEQID;
			parameters[3].Value = model.TPYE;
			parameters[4].Value = model.OPERA_MAN;
			parameters[5].Value = model.OPERA_TIME;
			parameters[6].Value = model.RECORD_TIME;
			parameters[7].Value = model.STATUS;
			parameters[8].Value = model.NOTE;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(rock.Model.ModelFEE_SHAOYAJING model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update FEE_SHAOYAJING set ");
			strSql.Append("RENT_CONTRACT_SEQID=@RENT_CONTRACT_SEQID,");
			strSql.Append("MONEY=@MONEY,");
			strSql.Append("RENT_CUSTOM_SEQID=@RENT_CUSTOM_SEQID,");
			strSql.Append("TPYE=@TPYE,");
			strSql.Append("OPERA_MAN=@OPERA_MAN,");
			strSql.Append("OPERA_TIME=@OPERA_TIME,");
			strSql.Append("RECORD_TIME=@RECORD_TIME,");
			strSql.Append("STATUS=@STATUS,");
			strSql.Append("NOTE=@NOTE");
			strSql.Append(" where SEQID=@SEQID ");
			SqlParameter[] parameters = {
					new SqlParameter("@RENT_CONTRACT_SEQID", SqlDbType.Int,4),
					new SqlParameter("@MONEY", SqlDbType.Decimal,9),
					new SqlParameter("@RENT_CUSTOM_SEQID", SqlDbType.Int,4),
					new SqlParameter("@TPYE", SqlDbType.VarChar,30),
					new SqlParameter("@OPERA_MAN", SqlDbType.VarChar,30),
					new SqlParameter("@OPERA_TIME", SqlDbType.DateTime),
					new SqlParameter("@RECORD_TIME", SqlDbType.DateTime),
					new SqlParameter("@STATUS", SqlDbType.Int,4),
					new SqlParameter("@NOTE", SqlDbType.VarChar,300),
					new SqlParameter("@SEQID", SqlDbType.Int,4)};
			parameters[0].Value = model.RENT_CONTRACT_SEQID;
			parameters[1].Value = model.MONEY;
			parameters[2].Value = model.RENT_CUSTOM_SEQID;
			parameters[3].Value = model.TPYE;
			parameters[4].Value = model.OPERA_MAN;
			parameters[5].Value = model.OPERA_TIME;
			parameters[6].Value = model.RECORD_TIME;
			parameters[7].Value = model.STATUS;
			parameters[8].Value = model.NOTE;
			parameters[9].Value = model.SEQID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
        public bool Delete(int SEQID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from FEE_SHAOYAJING ");
			strSql.Append(" where SEQID=@SEQID ");
			SqlParameter[] parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4)			};
			parameters[0].Value = SEQID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string SEQIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from FEE_SHAOYAJING ");
			strSql.Append(" where SEQID in ("+SEQIDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
        public rock.Model.ModelFEE_SHAOYAJING GetModel(int SEQID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 SEQID,RENT_CONTRACT_SEQID,MONEY,RENT_CUSTOM_SEQID,TPYE,OPERA_MAN,OPERA_TIME,RECORD_TIME,STATUS,NOTE from FEE_SHAOYAJING ");
			strSql.Append(" where SEQID=@SEQID ");
			SqlParameter[] parameters = {
					new SqlParameter("@SEQID", SqlDbType.Int,4)			};
			parameters[0].Value = SEQID;

			rock.Model.ModelFEE_SHAOYAJING model=new rock.Model.ModelFEE_SHAOYAJING();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["SEQID"]!=null && ds.Tables[0].Rows[0]["SEQID"].ToString()!="")
				{
					model.SEQID=int.Parse(ds.Tables[0].Rows[0]["SEQID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RENT_CONTRACT_SEQID"]!=null && ds.Tables[0].Rows[0]["RENT_CONTRACT_SEQID"].ToString()!="")
				{
					model.RENT_CONTRACT_SEQID=int.Parse(ds.Tables[0].Rows[0]["RENT_CONTRACT_SEQID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["MONEY"]!=null && ds.Tables[0].Rows[0]["MONEY"].ToString()!="")
				{
					model.MONEY=decimal.Parse(ds.Tables[0].Rows[0]["MONEY"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RENT_CUSTOM_SEQID"]!=null && ds.Tables[0].Rows[0]["RENT_CUSTOM_SEQID"].ToString()!="")
				{
					model.RENT_CUSTOM_SEQID=int.Parse(ds.Tables[0].Rows[0]["RENT_CUSTOM_SEQID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["TPYE"]!=null && ds.Tables[0].Rows[0]["TPYE"].ToString()!="")
				{
					model.TPYE=ds.Tables[0].Rows[0]["TPYE"].ToString();
				}
				if(ds.Tables[0].Rows[0]["OPERA_MAN"]!=null && ds.Tables[0].Rows[0]["OPERA_MAN"].ToString()!="")
				{
					model.OPERA_MAN=ds.Tables[0].Rows[0]["OPERA_MAN"].ToString();
				}
				if(ds.Tables[0].Rows[0]["OPERA_TIME"]!=null && ds.Tables[0].Rows[0]["OPERA_TIME"].ToString()!="")
				{
					model.OPERA_TIME=DateTime.Parse(ds.Tables[0].Rows[0]["OPERA_TIME"].ToString());
				}
				if(ds.Tables[0].Rows[0]["RECORD_TIME"]!=null && ds.Tables[0].Rows[0]["RECORD_TIME"].ToString()!="")
				{
					model.RECORD_TIME=DateTime.Parse(ds.Tables[0].Rows[0]["RECORD_TIME"].ToString());
				}
				if(ds.Tables[0].Rows[0]["STATUS"]!=null && ds.Tables[0].Rows[0]["STATUS"].ToString()!="")
				{
					model.STATUS=int.Parse(ds.Tables[0].Rows[0]["STATUS"].ToString());
				}
				if(ds.Tables[0].Rows[0]["NOTE"]!=null && ds.Tables[0].Rows[0]["NOTE"].ToString()!="")
				{
					model.NOTE=ds.Tables[0].Rows[0]["NOTE"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SEQID,RENT_CONTRACT_SEQID,MONEY,RENT_CUSTOM_SEQID,TPYE,OPERA_MAN,OPERA_TIME,RECORD_TIME,STATUS,NOTE ");
			strSql.Append(" FROM FEE_SHAOYAJING ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" SEQID,RENT_CONTRACT_SEQID,MONEY,RENT_CUSTOM_SEQID,TPYE,OPERA_MAN,OPERA_TIME,RECORD_TIME,STATUS,NOTE ");
			strSql.Append(" FROM FEE_SHAOYAJING ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

       

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public PageDataSet GetListByPage(String where, string orderby, int pageNo, int pageSize)
        {
            String sql = @"
                    SELECT 
                    FEE_SHAOYAJING.* ,
                    RENT_CONTRACT.CONTRACT_NUMBER,
                    RENT_CUSTOM.NAME AS CUSTOM_NAME,
                    HOUSE_ROOM.HOUSE_NUMBER AS HOUSE_NAME,
                    TEMP.XIAOQU_NAME ,
                    TEMP.DONG_NAME
                    FROM FEE_SHAOYAJING 
                    LEFT JOIN RENT_CONTRACT  ON FEE_SHAOYAJING.RENT_CONTRACT_SEQID = RENT_CONTRACT.SEQID
                    LEFT JOIN RENT_CUSTOM  ON RENT_CONTRACT.RENT_CUSTOM_SEQID =  RENT_CUSTOM.SEQID
                    LEFT JOIN HOUSE_ROOM ON RENT_CONTRACT.HOUSE_ROOM_SEQID =  HOUSE_ROOM.SEQID
                    LEFT JOIN  
                    (
                    SELECT  HOUSE_XIAOQU.NAME as XIAOQU_NAME , HOUSE_DONG.NAME AS DONG_NAME, ALL_FLOOR_START,ALL_FLOOR_END , HAS_DIANTI ,
                    HOUSE_XIAOQU.SEQID  as XIAOQU_SEQID , HOUSE_DONG.SEQID  as DONG_SEQID 
                    FROM HOUSE_XIAOQU , HOUSE_DONG WHERE HOUSE_XIAOQU.SEQID = HOUSE_DONG.HOUSE_XIAOQU_SEQID
                    ) AS TEMP ON HOUSE_ROOM.HOUSE_DONG_SEQID = TEMP.DONG_SEQID  ";
            if (where != null && !where.Equals(""))
            {
                sql += " WHERE " + where;
            }

            PageDataSet pds = new PageDataSet();
            pds.AllRecord = DbHelperSQL.GetRecordCount(sql);
            pds.DataSet = DbHelperSQL.GetListByPage(sql, orderby, pageNo, pageSize);

            return pds;

        }
 

        /// <summary>
        /// YajingType=0时取YajingMoney
        /// 否则取YajingMoney*计租单价
        /// </summary>
        /// <param name="YajingType"></param>
        /// <param name="YajingMoney"></param>
        /// <returns></returns>
        public DataSet GetShaoYajingNotifyMessage(int YajingType, int YajingMoney)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@YajingType", SqlDbType.Int, 4),
                     new SqlParameter("@YajingMoney", SqlDbType.Int, 4)
                                        };
            parameters[0].Value = YajingType;
            parameters[1].Value = YajingMoney;
            return DbHelperSQL.RunProcedure("GetYajingNotifyMessage", parameters, "ds");
        }

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "FEE_SHAOYAJING";
			parameters[1].Value = "SEQID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

